{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube('0CZ7Aj8hCyo')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Update the geemap package\n",
    "\n",
    "If you run into errors with this notebook, please uncomment the line below to update the [geemap](https://github.com/giswqs/geemap#installation) package to the latest version from GitHub. \n",
    "Restart the Kernel (Menu -> Kernel -> Restart) to take effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NAIP: National Agriculture Imagery Program\n",
    "\n",
    "The National Agriculture Imagery Program (NAIP) acquires aerial imagery during the agricultural growing seasons in the continental U.S.\n",
    "\n",
    "NAIP projects are contracted each year based upon available funding and the FSA imagery acquisition cycle. Beginning in 2003, NAIP was acquired on a 5-year cycle. 2008 was a transition year, and a three-year cycle began in 2009.\n",
    "\n",
    "NAIP imagery is acquired at a **one-meter** ground sample distance (GSD) with a horizontal accuracy that matches within six meters of photo-identifiable ground control points, which are used during image inspection.\n",
    "\n",
    "Older images were collected using 3 bands (Red, Green, and Blue: RGB), but newer imagery is usually collected with an additional near-infrared band (RGBN). \n",
    "\n",
    "More information about NAIP imagery can be found on [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets/catalog/USDA_NAIP_DOQQ)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create annual composite of NAIP imagery \n",
    "\n",
    "Select 4-band (RGBN) NAIP imagery."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "naip_ts = geemap.naip_timeseries(start_year=2009, end_year=2018)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a list of layer names to be shown under the dropdown list. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "layer_names = ['NAIP ' + str(year) for year in range(2009, 2019)]\n",
    "print(layer_names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set visualization parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "naip_vis = {'bands': ['N', 'R', 'G']}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a split-panel map for visualizing NAIP imagery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.ts_inspector(left_ts=naip_ts, right_ts=naip_ts, left_names=layer_names, right_names=layer_names, left_vis=naip_vis, right_vis=naip_vis)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create annual composite of Landsat imagery\n",
    "\n",
    "Use the drawing tools to create an ROI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region = Map.draw_last_feature\n",
    "roi = region.geometry()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# roi = ee.Geometry.Polygon(\n",
    "#     [[[-115.897448, 35.640766], \n",
    "#       [-115.897448, 36.603608], \n",
    "#       [-113.784915, 36.603608], \n",
    "#       [-113.784915, 35.640766], \n",
    "#       [-115.897448, 35.640766]]], None, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(roi.getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_ts = geemap.landsat_timeseries(roi=roi, start_year=1984, end_year=2019, start_date='01-01', end_date='12-31')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "layer_names = ['Landsat ' + str(year) for year in range(1984, 2020)]\n",
    "print(layer_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_vis = {\n",
    "    'min': 0,\n",
    "    'max': 4000,\n",
    "    'gamma': [1, 1, 1],\n",
    "    'bands': ['NIR', 'Red', 'Green']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.ts_inspector(left_ts=landsat_ts, right_ts=landsat_ts, left_names=layer_names, right_names=layer_names, left_vis=landsat_vis, right_vis=landsat_vis)\n",
    "Map.centerObject(roi, zoom=8)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare Landsat imagery and National Land Cover Database (NLCD)\n",
    "\n",
    "More information about NLCD can be found at the [Earth Engine Data Catalog](https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLCD = ee.ImageCollection('USGS/NLCD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLCD_layers = NLCD.aggregate_array('system:id').getInfo()\n",
    "print(NLCD_layers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLCD2001 = ee.Image('USGS/NLCD/NLCD2001').select('landcover')\n",
    "NLCD2006 = ee.Image('USGS/NLCD/NLCD2006').select('landcover')\n",
    "NLCD2011 = ee.Image('USGS/NLCD/NLCD2011').select('landcover')\n",
    "NLCD2016 = ee.Image('USGS/NLCD/NLCD2016').select('landcover')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLCD = ee.ImageCollection([NLCD2001, NLCD2006, NLCD2011, NLCD2016])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NLCD_layer_names = ['NLCD ' + str(year) for year in range(2001, 2017, 5)]\n",
    "print(NLCD_layer_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = ee.Geometry.Polygon(\n",
    "    [[[-115.897448, 35.640766], \n",
    "      [-115.897448, 36.603608], \n",
    "      [-113.784915, 36.603608], \n",
    "      [-113.784915, 35.640766], \n",
    "      [-115.897448, 35.640766]]], None, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_ts = geemap.landsat_timeseries(roi=roi, start_year=2001, end_year=2016, start_date='01-01', end_date='12-31')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_layer_names = ['Landsat ' + str(year) for year in range(2001, 2017)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_vis = {\n",
    "    'min': 0,\n",
    "    'max': 4000,\n",
    "    'gamma': [1, 1, 1],\n",
    "    'bands': ['NIR', 'Red', 'Green']}\n",
    "\n",
    "nlcd_vis = {\n",
    "    'bands': ['landcover']\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.ts_inspector(left_ts=landsat_ts, right_ts=NLCD, left_names=landsat_layer_names, right_names=NLCD_layer_names, left_vis=landsat_vis, right_vis=nlcd_vis)\n",
    "Map.centerObject(roi, zoom=8)\n",
    "Map"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
